package com.briup.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.briup.domain.Slideshow;
import com.briup.result.Result;
import com.briup.service.SlideshowService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Project: jd2407-cms
 * Package: com.briup.web.controller
 * Author: XBai
 * Time: 2024/11/18 14:45
 */

@RestController
@RequestMapping("/slideshow")
public class SlideshowController {
    @Autowired
    SlideshowService slideshowService;

    @ApiOperation(value = "根据条件查询轮播图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", dataType = "int", required = true,  paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "int", required = true,  paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态值", paramType = "query"),
            @ApiImplicitParam(name = "desc", value = "描述信息", paramType = "query")
    })
    @GetMapping("/query")
    public Result<Page<Slideshow>> query(Integer pageNum, Integer pageSize, String status, String desc) {
        // 创建 LambdaQueryWrapper 实例
        LambdaQueryWrapper<Slideshow> lqw = new LambdaQueryWrapper<>();
        // 创建分页对象
        Page<Slideshow> page = new Page<>();
        page.setCurrent(pageNum); // 设置当前页码
        page.setSize(pageSize);  // 设置每页大小
        // 根据条件构建查询
        if (status != null && !status.isEmpty()) {
            lqw.eq(Slideshow::getStatus, status);
        }
        if (desc != null && !desc.isEmpty()) {
            lqw.like(Slideshow::getDescription, desc);
        }
        // 执行分页查询
        Page<Slideshow> slideshowPage = slideshowService.page(page, lqw);
        return Result.success(slideshowPage);
    }

    @ApiOperation(value = "根据id查询轮播图信息", notes = "用于更新时的数据回显")
    @GetMapping("/queryById/{id}")
    public Result<Slideshow> findById(@PathVariable Integer id) {
        Slideshow slideshow = slideshowService.getById(id);
        return Result.success(slideshow);
    }

    @ApiOperation("查询所有可以用的轮播图")
    @GetMapping("/queryAllEnable")
    public Result<List<Slideshow>> getAllEnable() {
        LambdaQueryWrapper<Slideshow> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Slideshow::getStatus,"启用");
        List<Slideshow> list = slideshowService.list(lqw);
        return Result.success(list);
    }

//    @ApiOperation(value = "删除指定轮播图")
//    @DeleteMapping("/deleteById/{id}")
//    public Result<String> deleteById(@PathVariable Integer id) {
//        slideshowService.removeById(id);
//        return Result.success("删除成功");
//    }

    @ApiOperation(value = "删除轮播图")
    @DeleteMapping("/deleteInBatch}")
    public Result<String> deleteInBatch(@RequestBody List<Integer> ids) {
        slideshowService.removeByIds(ids);
        return Result.success("删除成功");
    }

    @ApiOperation(value = "新增或更新轮播图", notes = "slideshow参数包 含id值则为更新，不包含i为新增")
    @PostMapping("/saveOrUpdate")
    public Result<String> savaOrUpdate(@RequestBody Slideshow slideshow) {
        slideshow.setUploadTime(new Date());
        slideshowService.saveOrUpdate(slideshow);
        return Result.success("操作成功");
    }
}
